class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int cnt = 0, ans = 0;
        int n = nums.size();
        for(int r = 0, l = 0; r < n; ++r)
        {
            cnt += 1 - nums[r];
            while(cnt > k)
            {
                cnt -= 1 - nums[l++];
            }
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};
